//
//  LFTabBarController.m
//  QingDaoCommunity
//
//  Created by 李学林 on 15/11/22.
//  Copyright © 2015年 teteTechnology. All rights reserved.
//

#import "LFTabBarController.h"
#import "LFFactory.h"

#define kTopViewHeight 44

@interface LFTabBarController ()

@property(nonatomic,strong) UIView *topView;

@property(nonatomic,strong) UIView *buttomView;

@end

@implementation LFTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 隐藏系统的tabbar
    self.tabBar.hidden = YES;

// 初始化底部的tabbar
    _buttomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreemHeight - kTabBarHeight, kScreemWidth, kTabBarHeight)];
    
    self.buttomView.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:_buttomView];
    
    
    //
    for (int i = 0; i < 5; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *norImageStr = [NSString stringWithFormat:@"home_%d",i];
        
        [btn setBackgroundImage:[UIImage imageNamed:norImageStr] forState:UIControlStateNormal];
        
        NSString *pressedImageStr = [NSString stringWithFormat:@"home_%d_pressed",i];
        
        [btn setBackgroundImage:[UIImage imageNamed:pressedImageStr] forState:UIControlStateSelected];
        btn.tag = i;
        
        btn.frame = CGRectMake(i * kScreemWidth/5, 0, kScreemWidth/5.0, kTabBarHeight);
        
        [btn addTarget:self action:@selector(changeVC:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttomView addSubview:btn];
        
        if (i == 0) {
            
            btn.selected = YES;
        }
        
    }

    //初始化顶部的tabbar
    self.topView  = [[UIView alloc]initWithFrame:CGRectMake(0, kScreemHeight - kTabBarHeight - kTopViewHeight, kScreemWidth, kTopViewHeight)];
    
    self.topView.backgroundColor = [UIColor yellowColor];
    
   
    [self.view addSubview:self.topView];
    
    _topView.hidden = YES;
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.topView.bounds];
    imageView.image = [UIImage imageNamed:@"home_topbar"];
    
    [self.topView addSubview:imageView];
    
    //添加顶部tabbar的按钮

//    UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    topBtn.frame = CGRectMake(10, 0, kScreemWidth * (130/640.0), kTopViewHeight);
//    [topBtn setTitle:@"摇一摇" forState:UIControlStateNormal];
//    [self.topView addSubview:topBtn];
    
    
    //添加摇一摇
    UIButton *topBtn = [LFFactory type:0 WithFrame:CGRectMake(10, 0,kScreemWidth * (130/640.0), kTopViewHeight) WithTitle:@"摇一摇" WithTarget:self WithAction:@selector(changeVC:)];
    
    topBtn.tag = 5;
    [self.topView addSubview:topBtn];
    
    
    // 添加XMLL
    UIButton *shake = [LFFactory type:0 WithFrame:CGRectMake(kScreemWidth * (180/640.0), 0, kScreemWidth * (150/640.0), kTopViewHeight) WithTitle:@"联系商家" WithTarget:self WithAction:@selector(changeVC:)];
    shake.tag = 6;
    [self.topView addSubview:shake];
    
    // 添加直播
    UIButton *onlive = [LFFactory type:0 WithFrame:CGRectMake(kScreemWidth * (360/640.0), 0, kScreemWidth * (100/640.0), kTopViewHeight) WithTitle:@"直播" WithTarget:self WithAction:@selector(changeVC:)];
    onlive.tag = 7;
    [self.topView addSubview:onlive];
    
    
    // 添加我们
    UIButton *us = [LFFactory type:0 WithFrame:CGRectMake(kScreemWidth * (500/640.0), 0, kScreemWidth * (100/640.0), kTopViewHeight) WithTitle:@"我们" WithTarget:self WithAction:@selector(changeVC:)];
    us.tag = 8;
    [self.topView addSubview:us];
    
}

// 切换控制器
- (void)changeVC:(UIButton*)btn{
    
    [self.buttomView.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj.selected = NO;
        
    }];
    
    
    btn.selected = YES;
    
    if (btn.tag < 4) {
        self.selectedIndex = btn.tag;
        
    }else if(btn.tag == 4 ){
        _topView.hidden = NO;
        
    }else{
    
        self.selectedIndex = btn.tag - 1;

    }
    
}

#pragma mark ========显示或者隐藏tabbarview
- (void)showOrHiddenTabbarView:(BOOL)flag
{

    if (flag) {
        self.topView.hidden = YES;
        self.buttomView.hidden = YES;
    }else{
    
        self.topView.hidden = NO;
        self.buttomView.hidden = NO;
    }



}

@end
